package com.baizhi.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1、获取session
        HttpSession session = request.getSession();
        //2、获取登陆标识
        Object user = session.getAttribute("login");
        System.out.println("自定义拦截器类执行了，登陆标识"+user);
        //3、判断放行与否
        if(user == null){
            //登陆失败  不放行并跳转到登陆页面
            response.sendRedirect(request.getContextPath()+"/login.html");
            return false;
        }
        return true;
    }
}
